<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-container">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;height:200px">
            <form class="layui-form" id="saveCustomerLevelForm">
                <input type="hidden" name="id" value="${cid}">

                <div class="layui-form-item">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="number" name="login"  placeholder="账号"
                               class="layui-input ">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">地址</label>
                    <div class="layui-col-sm2">
                        <select name="proId" data-type="2" data-click="20">
                            <option value="">请选择</option>
                            <c:forEach items="${addressList}" var="a">
                                <option value="${a.id}">${a.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="layui-col-sm2" style="margin-left: 10px">
                        <select class="address city" name="cityId" data-type="3" data-click="30">
                        </select>
                    </div>
                    <div class="layui-col-sm2" style="margin-left: 10px">
                        <select class="address area" name="areaId">
                        </select>
                    </div>
<%--                    <div class="layui-col-sm2" style="margin-left: 10px">--%>
<%--                        <input type="text" name="addressDetail" placeholder="详细地址"--%>
<%--                               class="layui-input address">--%>
<%--                    </div>--%>
                </div>

            </form>
            <div class="layui-form-item layui-layout-admin">
                <div class="layui-input-block">
                    <div class="layui-footer" style="left: 0;">
                        <button class="layui-btn" onclick="saveCustomerLevel()">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery.form.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script>
    layui.config({
        //静态资源所在路径
        base: '/layuiadmin/'
    }).extend({
        //主入口模块
        index: 'lib/index'
    }).use(['index', 'form'], function () {
        var $ = layui.$, layer = layui.layer, element = layui.element,form = layui.form;;

        form.on('select', function (data) {
            //得到select原始DOM对象
            var dom = data.elem;
            //得到选择下拉框类型，1为选择行业，2为选择地址
            var selectType = $(dom).data("type");
            //得到点击下拉框类型，10对应行业，20、30对应地址
            //此处是重新选择时需要清空其它下拉框的数据
            var clickType = $(dom).data('click');
            //得到被选中元素的id
            var id = data.value;
            //selectType为1时代表是选择行业

            //selectType为2或3一起处理，选择地址下拉框
            $.ajax({
                url: "/sys/queryAddressNoPage",
                data: {"pid": id},
                dataType: "json",
                type: "post",
                success: function (data) {
                    var html = '<option value="">请选择</option>';
                    $.each(data.data, function (i, v) {
                        html += '<option value="' + v.id + '" >' + v.name + '</option>'
                    });
                    if (selectType === 2) {
                        //城市
                        $(".city").append(html);
                    } else if (selectType === 3) {
                        //地区
                        $(".area").append(html);
                    }
                    form.render('select');
                }
            });

            //点击清空其它下拉框事件
            if (clickType === 20) {
                //重新选择了省份，清空城市、地区的下拉框
                $(".city").empty();
                $(".area").empty();
            } else if (clickType === 30) {
                //重新选择了城市，清空地区下拉框
                $(".area").empty();
            }
            //渲染表单中下拉框元素
            form.render('select');
        });


    });

    function saveCustomerLevel() {
        $.ajax({
            url: "/customer/handleCustomerEdit",
            data: $("#saveCustomerLevelForm").serialize(),
            dataType: "json",
            type: "post",
            success: function (data) {
                console.log(data)
                if (data.status) {
                    layer.msg('操作成功');
                    window.setTimeout(function () {
                        window.parent.location.reload();
                    }, 1000);
                } else {
                    layer.msg(data.desc);
                }
            }
        });
    }
</script>
</body>
</html>